//
//  PersonInfoViewController.m
//  YUXiu
//
//  Created by wang on 16/6/26.
//  Copyright © 2016年 Wang. All rights reserved.
//

@interface MyButton : UIButton

@end
@implementation MyButton

//100.30
-(CGRect)titleRectForContentRect:(CGRect)contentRect{
    CGRect titleFrame;
    titleFrame.origin.x = contentRect.origin.x + 22;
    titleFrame.origin.y = contentRect.origin.y;
    titleFrame.size.width = contentRect.size.width - 22;
    titleFrame.size.height = contentRect.size.height;
    return titleFrame;
}
-(CGRect)imageRectForContentRect:(CGRect)contentRect{
    CGRect imgRect;
    imgRect.origin.x = contentRect.origin.x;
    imgRect.origin.y = contentRect.origin.y + 5;
    imgRect.size = CGSizeMake(20, 20);
    return imgRect;
}
@end


#import "PersonInfoViewController.h"
#import "MytableHraderView.h"
#import "UserPlayerCollectionViewCell.h"
#import "CollectionLayout.h"
#import "GiftListViewController.h"
#import "LivePlayViewController.h"

@interface PersonInfoViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>{
    UICollectionView *collectView;
    NSArray *cellectionfalseData;
    UIView *pushView;
    BOOL isSelet;
}
@property(strong,nonatomic) UILabel *PlayState;
@end

@implementation PersonInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
}
-(void)autoConfigViews{
    cellectionfalseData = @[@"历史1",@"历史1",@"历史1",@"历史1",@"历史1"];
    self.navigationController.navigationBar.translucent = NO;
    [self initNav];
    UIView *view_bg = [[UIView alloc] initWithFrame:krect(0, -64, kScreenWidth, 64)];
    [self.view addSubview:view_bg];
    
    self.tableView.tableHeaderView = [self getHeaderView];
    
    [self.view bringSubviewToFront:self.tableView];
}
-(UIView *)getHeaderView{
    UIView *bgView = [[UIView alloc]initWithFrame:krect(0, 0, kScreenWidth, 214 + 64+44)];
    UIImageView *imgView = [[UIImageView alloc]initWithFrame:krect(0, 0, kScreenWidth, 278+44)];
    [imgView setImage:kCreatImage(@"001.jpg")];
    [bgView addSubview:imgView];
    //  创建需要的毛玻璃特效类型
    UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    //  毛玻璃view 视图
    UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
    //添加到要有毛玻璃特效的控件中
    effectView.frame = imgView.bounds;
    [imgView addSubview:effectView];
    //设置模糊透明度
    effectView.alpha = .9f;
    
    bgView.backgroundColor = kOrangeCOLOR;
    MytableHraderView * view= [[MytableHraderView alloc]initWithFrame:krect(30, 44, kScreenWidth-60, 214+64 + 20) model:self.flaseModel];
        [bgView insertSubview:view aboveSubview:effectView];
    return bgView;
}
//导航栏
- (void)initNav{
     //左边
//    UIButton *left = [[UIButton alloc]initWithFrame:krect(0, 0, 40, 20)];
//    [left setBackgroundColor:kOrangeCOLOR];
////    [right setBackgroundImage:kCreatImage(@"header") forState:UIControlStateNormal];
//    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithCustomView:left];
//    self.navigationItem.leftBarButtonItem = leftItem;
    
    UIButton *right = [[UIButton alloc]initWithFrame:krect(0, 0, 40, 20)];
    [right setBackgroundColor:kOrangeCOLOR];
    [right setTitle:@"展开" forState:UIControlStateNormal];
    right.tag = 11;
    [right addTarget:self  action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    //    [right setBackgroundImage:kCreatImage(@"header") forState:UIControlStateNormal];
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithCustomView:right];
    self.navigationItem.rightBarButtonItem = rightItem;
    
    _PlayState = [[UILabel alloc]initWithFrame:krect(kScreenWidth/2 + 60, 25, 40, 20)];
    _PlayState.backgroundColor = kOrangeCOLOR;
    _PlayState.layer.cornerRadius = 7;
    _PlayState.clipsToBounds = YES;
    _PlayState.text = @"直播中";
    _PlayState.textAlignment = NSTextAlignmentCenter;
    _PlayState.font = Font(10);
    [self.navigationController.view addSubview:_PlayState];
    
    
}

#pragma mark - tableView
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 50;
}
//分区头，礼物榜
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc]initWithFrame:krect(0, 0, kScreenWidth, 50)];
    view.backgroundColor = kNaviBarCOLOR;
    MyButton *giftButton = [[MyButton alloc]initWithFrame:krect(10, 10, 100, 30)];
    [giftButton setTitle:@"礼物贡献榜" forState:UIControlStateNormal];
    [giftButton setImage:kCreatImage(@"gift") forState:UIControlStateNormal];
    [giftButton.titleLabel setFont:Font(14)];
    giftButton.backgroundColor = kNaviBarCOLOR;
    giftButton.tag = 32;
    [giftButton addTarget:self action:@selector(reportBtn:) forControlEvents:UIControlEventTouchUpInside];
    [giftButton setTitleColor:kOrangeCOLOR forState:UIControlStateNormal];
    [view addSubview:giftButton];
    
    UIImageView *rightArrow = [[UIImageView alloc]initWithFrame:krect(kScreenWidth - 30-5, 10, 30, 30)];
    [rightArrow setImage:kCreatImage(@"arrow")];
    [view addSubview:rightArrow];
    
    for (int i = 0 ; i < 4; i++) {
        float kwidth = 36;
        UIImageView *hedImg = [[UIImageView alloc]initWithFrame:krect(kScreenWidth - 30 - (i + 1)*46, 7, kwidth, kwidth)];
        [hedImg setImage:kCreatImage(@"001.jpg")];
        hedImg.layer.cornerRadius = 18;
        hedImg.clipsToBounds = YES;
        [view addSubview:hedImg];
        hedImg.tag = 100 + i;
        //
        UITapGestureRecognizer *tapHed = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapFansHead:)];

        [hedImg addGestureRecognizer:tapHed];
        hedImg.userInteractionEnabled = YES;
    }
    
    return view;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 2;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        return 50;
    }else{
        //cell加载collectionView，自动设置高度
        NSInteger FB = cellectionfalseData.count;

            return (FB/2 *170 + FB%2 *170);
        
        
    }
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell0"];
        UILabel *la = [[UILabel alloc]initWithFrame:krect(0, 5, kScreenWidth, 40)];
        la.text = @"Ta的直播回放";
        la.font = Font(14);
        la.textColor = kBlackCOLOR;
        la.textAlignment = NSTextAlignmentCenter;
        [cell.contentView addSubview:la];
        return cell;
    }else{
        static NSString *identy = @"cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identy];
        if (!cell) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identy];
        }
        CollectionLayout *layout = [[CollectionLayout alloc]init];
        collectView = [[UICollectionView alloc]initWithFrame:krect(0, 0, kScreenWidth, ((cellectionfalseData.count/2 *170 + cellectionfalseData.count%2 *170)) *170) collectionViewLayout:layout];
        collectView.delegate = self;
        collectView.dataSource = self;
        collectView.scrollEnabled = NO;
        collectView.backgroundColor = kWhiteCOLOR;
        
        [cell.contentView addSubview:collectView];
        
        [collectView registerClass:[UserPlayerCollectionViewCell class] forCellWithReuseIdentifier:@"MyCell"];
        
        return cell;

    }
    
}
#pragma mark - collectionView delegate
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return cellectionfalseData.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    UserPlayerCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath];
    
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    [self showHUDWithText:[NSString stringWithFormat:@"点击的是第%ld个item",(long)indexPath.row] AfterDelay:1];
    LivePlayViewController *liveVC = [[LivePlayViewController alloc]init];
    [self.navigationController pushViewController:liveVC animated:YES];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [_PlayState setHidden:YES];
    self.navigationController.navigationBar.barTintColor = kNaviBarCOLOR;//导航栏颜色
    [self.navigationController.navigationBar setTranslucent:NO];
}

-(void)viewWillAppear:(BOOL)animated
{
    
    self.navigationItem.title = @"";
    [super viewWillAppear:YES];
    for (int i = 1; i < 3; i ++) {
        UIButton *btn = (UIButton *)[self.navigationController.view viewWithTag:1000 + i];
        btn.hidden = NO;
    }
    [self.navigationController.navigationBar setTranslucent:YES];
    if ([self.navigationController.navigationBar respondsToSelector:@selector(shadowImage)])
    {
        [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
        [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
    }
    [self.navigationController.navigationBar setBackgroundColor:[UIColor clearColor]];
    
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"bigShadow"] forBarMetrics:UIBarMetricsCompact];
    [_PlayState setHidden:NO];
    
}
#pragma mark - 手势事件
-(void)tapFansHead:(UITapGestureRecognizer *)ges{
    NSInteger tapNum;
    tapNum = ges.view.tag;
    NSLog(@"点击了第%ld个头像",(long)tapNum - 100);
}
-(void)click:(UIButton *)btn{
    
    if (!isSelet) {
        //右item
        if (pushView) {
            [pushView removeFromSuperview];
        }
        pushView = [[UIView alloc]initWithFrame:krect(kScreenWidth-90-10, -72, 90, 80)];
        UIButton *reportBtn = [[UIButton alloc]initWithFrame:krect(0, 0, 90, 39)];
        [reportBtn setTitle:@"举报" forState:UIControlStateNormal];
        [reportBtn setTitleColor:kBlackCOLOR forState:UIControlStateNormal];
        [reportBtn setBackgroundColor:kNaviBarCOLOR];
        reportBtn.tag = 30;
        [reportBtn addTarget:self action:@selector(reportBtn:) forControlEvents:UIControlEventTouchUpInside];
        [pushView addSubview:reportBtn];

        UIButton *blackList = [[UIButton alloc]initWithFrame:krect(0, 41, 90, 39)];
        [blackList setTitle:@"拉黑" forState:UIControlStateNormal];
        [blackList setTitleColor:kBlackCOLOR forState:UIControlStateNormal];
        [blackList addTarget:self action:@selector(reportBtn:) forControlEvents:UIControlEventTouchUpInside];
        blackList.tag = 31;
        [blackList setBackgroundColor:kNaviBarCOLOR];

        [pushView addSubview:blackList];

        [UIView animateWithDuration:0.5 animations:^{
            [pushView setFrame:krect(kScreenWidth-90-10, 72, 90, 80)];
        }];
        [self.view addSubview:pushView];
        [btn setTitle:@"收起" forState:UIControlStateNormal];
    }else{
        [UIView animateWithDuration:0.5 animations:^{
            [pushView setFrame:krect(kScreenWidth-90-10, -72, 90, 80)];
        } completion:^(BOOL finished) {
            [pushView removeFromSuperview];
            [btn setTitle:@"展开" forState:UIControlStateNormal];

        }];
    }
    isSelet = !isSelet;
}
-(void)reportBtn:(UIButton *)sender{
    if (sender.tag == 30) {
        //举报
    }else if(sender.tag == 31){
        //拉黑tag=31
    }else{//tag 32
        //跳转礼物榜
        GiftListViewController *goVC = [[GiftListViewController alloc]init];
        [self.navigationController pushViewController:goVC animated:YES];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

 UIView *view = [[UIView alloc] initWithFrame:Rect(0, 0, kScreenWidth, kScreenHeight)];
 view.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
 view.tag = 9000;
 //添加缩放动画
 paymentView.transform = CGAffineTransformMakeScale(0, 0);
 [view addSubview:paymentView];
 [UIView animateWithDuration:0.5 animations:^{
 paymentView.transform = CGAffineTransformMakeScale(1, 1);
 }];
 [self.view addSubview:view];

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
